a = 2  # 每格暂定的px值，另一个该值在operation中

#我加这一句话为了测试一下GitEE

def isPointinPolygon(point, rangelist):
    # 判断是否在外包矩形内，如果不在，直接返回false
    lnglist = []#横坐标
    latlist = []#纵坐标


    for i in range(len(rangelist) - 1):
        lnglist.append(rangelist[i][0])
        latlist.append(rangelist[i][1])
    #print(lnglist, latlist)
    maxlng = max(lnglist)
    minlng = min(lnglist)
    maxlat = max(latlist)
    minlat = min(latlist)
    #print(maxlng, minlng, maxlat, minlat)
    if (point[0] > maxlng or point[0] < minlng or
            point[1] > maxlat or point[1] < minlat):
        return False
    count = 0
    point1 = rangelist[0]
    for i in range(1, len(rangelist)):
        point2 = rangelist[i]
        # 点与多边形顶点重合
        if (point[0] == point1[0] and point[1] == point1[1]) or (point[0] == point2[0] and point[1] == point2[1]):
         #   print("在顶点上")
            return False
        # 判断线段两端点是否在射线两侧 不在肯定不相交 射线（-∞，lat）（lng,lat）
        if (point1[1] < point[1] and point2[1] >= point[1]) or (point1[1] >= point[1] and point2[1] < point[1]):
            # 求线段与射线交点 再和lat比较
            point12lng = point2[0] - (point2[1] - point[1]) * (point2[0] - point1[0]) / (point2[1] - point1[1])
       #     print(point12lng)
            # 点在多边形边上
            if (point12lng == point[0]):
       #         print("点在多边形边上")
                return False
            if (point12lng < point[0]):
                count += 1
        point1 = point2
  #  print(count)
    if count % 2 == 0:
        return False
    else:
        return True

#将图形转化为二维数组
def array2d(points):

    p = [[0 for i in range(500)] for i in range(500)]
    for i in range(500):
        for j in range(500):
            test_point = [j * a + a / 2, i * a + a / 2]  # 让误差小一点，使用小方格的中间作为判定依据
            if isPointinPolygon(test_point, points):
                p[i][j] = 1;

    return p




#多边形面积
def multi_area(points):
    area = 0
    q = points[-1]
    for p in points:
        area += p[0] * q[1] - p[1] * q[0]
        q = p
    return abs(area / 2)



